# -*- coding: utf-8 -*-

from gtk import *
from includes import *
import cairo

class Preview(DrawingArea):
	
	__gsignals__ = { "expose-event": "override" }
	
	def __init__(self, ppm):
		DrawingArea.__init__(self)
		self.ppm = ppm
		self.pbs = []
		self.lignes = 1
		self.colonnes = 1
	
	def do_expose_event(self, event):
		
		(width, height) = self.window.get_size()
		width_per_image = float(width) / self.colonnes
		height_per_image = float(height) / self.lignes
		ratio = width_per_image / height_per_image
		
		posx = 0
		posy = - height_per_image
		
		for current,pb in enumerate(self.pbs):
			cr = self.window.cairo_create()
			
			image_width = pb.get_width()
			image_height = pb.get_height()
			
			if (current % self.colonnes) == 0:
				posx = 0
				posy += height_per_image
			else:
				posx += width_per_image
			cr.translate(posx, posy)
			
			if (float(image_width) / image_height) > ratio:
				r = width_per_image / image_width
				cr.scale(r, r)
				t = ((height_per_image / r) - image_height) / 2.0
				cr.translate(0, t)
				cr.rectangle(0, 0, image_width, image_height)
				cr.clip_preserve()
			else:
				r = height_per_image / image_height
				cr.scale(r, r)
				t = ((width_per_image / r) - image_width) / 2.0
				cr.translate(t, 0)
				cr.rectangle(0, 0, image_width, image_height)
				cr.clip_preserve()
				
			cr.set_source_pixbuf(pb, 0, 0)
			cr.fill()
					
